﻿// ------------------------------------------------------------------------------------------------
// <copyright file="FileManager.cs" company="Hiyoko">
//   Copyright (c) 2015 All Rights Reserved
// </copyright>
// <summary>
//   Yep. I created a class in order to manage the plugin temporary files and paths.
//   Next step is to implement something smarter. Ideas?
// </summary>
// ---------------------------------------------------------------------------------------------

using System;
using System.IO;

namespace Hiyoko.SolutionsComparer.Manager
{
    public class FileManager
    {
        #region Private members

        /// <summary>
        /// The Root folder name
        /// </summary>
        private static readonly string RootDirectory = Path.Combine(Path.GetTempPath(), "SolutionsComparer");

        /// <summary>
        /// Unique identifier
        /// </summary>
        private readonly string _uniqueIdentifier = Guid.NewGuid().ToString("D");

        /// <summary>
        /// The Working directory
        /// </summary>
        private string WorkingDirectory
        {
            get
            {
                return Path.Combine(RootDirectory, _uniqueIdentifier);
            }
        }

        #endregion Private members

        #region Public members

        /// <summary>
        /// The source working directory
        /// </summary>
        public string SourceDirectory
        {
            get { return Path.Combine(WorkingDirectory, "Source"); }
        }

        /// <summary>
        /// The changed working directory
        /// </summary>
        public string ChangedDirectory
        {
            get { return Path.Combine(WorkingDirectory, "Changed"); }
        }

        /// <summary>
        /// The output working directory
        /// </summary>
        public string OutputDirectory
        {
            get { return Path.Combine(WorkingDirectory, "Output"); }
        }

        /// <summary>
        /// The source Zip file path
        /// </summary>
        public string SourceZipFilePath
        {
            get { return Path.Combine(SourceDirectory, string.Format("{0}.zip", "solution")); }
        }

        /// <summary>
        /// The changed Zip file path
        /// </summary>
        public string ChangedZipFilePath
        {
            get { return Path.Combine(ChangedDirectory, string.Format("{0}.zip", "solution")); }
        }

        /// <summary>
        /// The source customizations file path
        /// </summary>
        public string SourceCustomizationsFilePath
        {
            get { return Path.Combine(SourceDirectory, "customizations.xml"); }
        }

        /// <summary>
        /// The changed customizations file path
        /// </summary>
        public string ChangedCustomizationsFilePath
        {
            get { return Path.Combine(ChangedDirectory, "customizations.xml"); }
        }

        /// <summary>
        /// The XML diff gram file path
        /// </summary>
        public string DiffGramFilePath
        {
            get { return Path.Combine(OutputDirectory, "diffGram.xml"); }
        }

        /// <summary>
        /// The HTML result file path
        /// </summary>
        public string HtmlResultPath
        {
            get { return Path.Combine(OutputDirectory, "result.html"); }
        }

        /// <summary>
        /// The HTML minified result file path
        /// </summary>
        public string HtmlDiffResultPath
        {
            get { return Path.Combine(OutputDirectory, "result.diff.html"); }
        }

        #endregion Public members

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the FileManager class.
        /// </summary>
        public FileManager()
        {
            Directory.CreateDirectory(SourceDirectory);
            Directory.CreateDirectory(ChangedDirectory);
            Directory.CreateDirectory(OutputDirectory);
        }

        #endregion Constructor

        #region Public methods

        /// <summary>
        /// Delete temporary files
        /// </summary>
        public static void DeleteTempFiles()
        {
            var rootDirectoryPath = Path.Combine(Path.GetTempPath(), "SolutionsComparer");
            if (Directory.Exists(rootDirectoryPath))
            {
                Directory.Delete(rootDirectoryPath, true);
            }
        }

        /// <summary>
        /// Copy source file to destination
        /// </summary>
        /// <param name="sourcePath">The source file path</param>
        /// <param name="targetPath">THe target file path</param>
        public static void CopyFile(string sourcePath, string targetPath)
        {
            if (string.IsNullOrEmpty(sourcePath))
            {
                throw new ArgumentException("The sourcePath argument is null or empty.");
            }

            if (string.IsNullOrEmpty(targetPath))
            {
                throw new ArgumentException("The targetPath argument is null or empty.");
            }

            File.Copy(sourcePath, targetPath, true);
        }

        #endregion Public methods
    }
}
